package com.snda.lib.task;

import com.snda.lib.http.HttpConst;
import com.snda.lib.http.ICallBack;
import android.content.Context;

/*
 * 从服务器下载文件
 */
public class DownloadFileTask extends DownloadAsyncTask {

	protected long nTotalSize = 0;
	protected int nCurrentPercent = 0;

	public DownloadFileTask(Context context, ICallBack callback) {
		super(context, callback);

		this.nTaskType = HttpConst.TASKTYPE_DOWNLOADFILE;
	}

	/*
	 * 通知当前需要下载的内容的总长度
	 * 
	 * @param nTotalSize 返回的需要下载的总长度
	 */
	@Override
	public void OnGetSize(long nTotalSize) {
		this.nTotalSize = nTotalSize;
		if (this.nTotalSize < 0)
			this.nTotalSize = 0;
	}

	@Override
	public boolean OnStartTask(String strUrl, String strSavePath) {
		return true;
	}

	/*
	 * 通知下载任务开始
	 */
	@Override
	public void OnBeginTask(String strUrl, long nFinishedSize,
			String strSavePath) {
	}

	/*
	 * 通知当前的下载进度
	 * 
	 * @param nFinishedSize 已经下载的总长度
	 */
	@Override
	public void OnNotifyProcess(long nFinishedSize) {
		publishProgress(nFinishedSize);

		int nPercent = (int) (nFinishedSize * 100 / nTotalSize);
		if (nPercent != nCurrentPercent) {
			nCurrentPercent = nPercent;
		}
	}

	/*
	 * 通知当前的任务已经下载完毕
	 */
	@Override
	public void OnFinishedTask(String strDownloadPath) {
		if (strDownloadPath == null)
			return;

		nCurrentPercent = 100;
	}

	/*
	 * 通知当前的任务下载发生失败
	 */
	@Override
	public void OnFailed(int nErrorCode) {
	}
}
